﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace FinalTestMachine.View.MainUnits
{
    /// <summary>
    /// Wnd_SocketSNCheckWindow.xaml 的交互逻辑
    /// </summary>
    public partial class Wnd_SocketSNCheckWindow : Window
    {
        static Wnd_SocketSNCheckWindow Cur = null;
        static readonly object ob_lock = new object();
        private Wnd_SocketSNCheckWindow()
        {
            InitializeComponent();

            InitSelected();
        }

        void InitSelected()
        {
            for (int i = 0; i < GlobalParams.WorkstationSum; i++)
            {
                comb_ID.Items.Add((char)(65+i));
            }
        }

        public static Wnd_SocketSNCheckWindow CreateInstrance()
        {
            if(Cur==null)
            {
                lock (ob_lock)
                {
                    if(Cur==null)
                    {
                        Cur = new Wnd_SocketSNCheckWindow();
                    }

                }
            }

            return Cur;
            
        }

        public static void SingleShow()
        {
            Window window = CreateInstrance();
            window.WindowState = WindowState.Normal;
            window.Show();
            window.Focus();
        }

        bool IsCanScan = false;

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                lv_Carrier.ItemsSource = SocketSNSetting.Cur.SocketList;
                Connect.ConnectFactory.TCP_ScanSocket.RecevieMsgActionEventHander += SerialPort_ScanSocket_RecevieMsgActionEventHander;


            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
        }
        private void Window_Closed(object sender, EventArgs e)
        {
            Cur = null;
           Motion.IO.StaticIOHelper.Out_SocketScanTrigger.Out_OFF();
            Connect.ConnectFactory.TCP_ScanSocket.RecevieMsgActionEventHander -= SerialPort_ScanSocket_RecevieMsgActionEventHander;
        }
        private void SerialPort_ScanSocket_RecevieMsgActionEventHander(string obj)
        {
            this.Dispatcher.Invoke(()=> 
            {
                //if(IsCanScan)
                //{
                    tb_CarrierName.Text = obj;
                //}
                //IsCanScan = false;
            });
        }

        private void Btn_add_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if(string.IsNullOrEmpty(tb_CarrierName.Text))
                {
                    throw new Exception("请先输入Socket 编码！");
                }

                if(comb_ID.SelectedItem==null)
                {
                    throw new Exception("请选择对应的ID");
                }
                SocketSNSetting.Cur.Add(tb_CarrierName.Text, comb_ID.SelectionBoxItem.ToString());
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
        }

        private void Btn_delete_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if(lv_Carrier.SelectedItem!=null)
                {
                    Param_Socket param_Socket = lv_Carrier.SelectedItem as Param_Socket;
                    MessageBoxResult result=  MessageBox.Show($"确定删除：SocketSN={param_Socket.SN},ID={param_Socket.ID}", "删除警告", MessageBoxButton.YesNo, MessageBoxImage.Warning);
                   
                    if(result==MessageBoxResult.Yes)
                    {
                        SocketSNSetting.Cur.Remov(param_Socket);
                    }
                
                }
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
        }

        private void Btn_scan_Click(object sender, RoutedEventArgs e)
        {
            IsCanScan = true;

           Motion.IO.StaticIOHelper.Out_SocketScanTrigger.Out_ON();
        }

        private void Btn_scanOff_Click(object sender, RoutedEventArgs e)
        {
            IsCanScan = false;

           Motion.IO.StaticIOHelper.Out_SocketScanTrigger.Out_OFF();
        }

        private void Btn_rename_Click(object sender, RoutedEventArgs e)
        {
            try
            {

            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
        }

        private void Btn_save_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                SocketSNSetting.Cur.Save();
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
        }
    }
}
